package com.mk.service.web;

import act.controller.Controller;
import cn.hutool.core.date.DateUtil;
import com.mk.domain.ZMbAddress;
import com.mk.interceptor.ITranslate;
import com.mk.util.S2;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.mvc.annotation.With;
import org.osgl.util.S;

import javax.inject.Inject;
import java.util.List;

@Controller("address")
/**
 * 收货地址相关类
 */
public class ZMbAddressService extends BaseWebService {

    @Inject
    private ZMbAddress.Mapper addressMapper;

    /**
     * 保存我的收货地址信息
     */
    @PostAction("save.json")
    @With({WbILogin.class, ITranslate.class})
    public String save(ZMbAddress address){
        address.setMid(me.getMid());
        address.setCreatedate(DateUtil.now());
        if(S.eq(address.getFlag(),"0")){
            addressMapper.executeUpdate("update z_mb_address set flag='1' where mid=?", me.getMid());
        }else {
            address.setFlag("1");
        }
        if(S.isBlank(address.getAddid())){
            address.setAddid(S2.UUID());
            addressMapper.insert(address);
        }else{
	        addressMapper.updateTemplateById(address);
	    }
	    return address.getAddid();
    }


    /**
     * 我的地址列表
     * @return
     */
    @GetAction("list.json")
    @With(WbILogin.class)
    public List<ZMbAddress> list(){
        ZMbAddress address = new ZMbAddress();
        address.setMid(me.getMid());
        List<ZMbAddress> list = addressMapper.template(address);
        return list;
    }

    /**
     * 我的默认地址
     * @return
     */
    @GetAction("default.json")
    @With(WbILogin.class)
    public ZMbAddress defaultAddress(){
//        ZMbAddress address = new ZMbAddress();
//        address.setMid(me.getMid());
//        address.setFlag("0");
//        ZMbAddress result = addressMapper.templateOne(address);
        ZMbAddress result = addressMapper.getDefaultAddress(me.getMid());
        if(result!=null){
            return result;
        }
        return new ZMbAddress();
    }

    /**
     * 删除地址
     * @return
     */
    @DeleteAction("deleteById.json")
    @With(WbILogin.class)
    public Integer deleteById(String addid){
        return addressMapper.deleteById(addid);
    }

}
